#!/bin/bash

TOOL_DIR=$(cd "$(dirname "$0")"; pwd)
WORKSPACE_DIR=$(dirname "$TOOL_DIR")

POM_FILE=
TARGET_VERSION=

while getopts "hv:" opt; do
    case "$opt" in
    h)
        printf "SYNOPSIS\n"
        printf "    reflash_pom_version [OPTIONS]\n"
        printf "DESCRIPTION\n"
        printf "    The pom.xml is copied to remote repository without variable translation when mvn deploy. So before deply, it is need to replace the version to a real version.\n"
        printf "OPTIONS\n"
        printf "    -h\n"
        printf "        Print a usage message briefly summarizing these command-line options, then exit.\n"
        printf "    -v <version>\n"
        printf "        Specify the real target version to replace. The version in pom.xml will be replaced to this value.\n"
        printf "\n"
        exit 1
        ;;
    v)  TARGET_VERSION=$OPTARG-SNAPSHOT
        #printf "[INFO] b $opt $OPTARG   $OPTIND\n"
        ;;
    esac
done

if [ "$TARGET_VERSION" =  "" ]; then
    printf "[ERROR] The target version not found in command  \'$0 $*\', please use -h option to check the usage.\n"
    exit 2
fi
printf "[INFO] Parameter is set to command, WORKSPACE_DIR=$WORKSPACE_DIR, TARGET_VERSION=$TARGET_VERSION\n"

property_file=${WORKSPACE_DIR}/pom.properties
printf "[INFO] Replace ehapp.version to target version in pom.properties, path=$property_file\n"

"${JAVA_HOME}/bin/java" -jar ${WORKSPACE_DIR}/tools/pom-sync-2.0-SNAPSHOT.jar -R -Dehapp.version=${TARGET_VERSION} -e ${property_file} ${WORKSPACE_DIR}/
